<template>
  <div>
    <vxe-modal
      :z-index="10"
      show-zoom
      :title="title"
      :loading="loadingmodal1"
      ref="modal1"
      :resize="true"
      :showFooter="true"
      :mask-closable="true"
      destroy-on-close
      width="600"
      height="90vh"
    >
      <template v-slot>
        <!-- <depart-user v-if="'user'===type" ref="departUser" :company-id="companyId" :disable-user="tableData2"
                     @addDepartUser="addDepartUser"></depart-user> -->
                     <!-- 直接改为角色展示，不按照类型展示。2023年11月16日15:15:21 -->
        <sys-role  v-if="'role'===type" ref="sysRole" @addRole="addRole" :company-id="companyId"></sys-role>
        <!-- <sys-depart ref="sysDepart" @addDepart="addDepart" :company-id="companyId" v-if="'dept'===type"></sys-depart> -->
      </template>
      <template v-slot:footer>
        <vxe-button @click="$refs.modal1.close()">关闭</vxe-button>
        <vxe-button status="primary" v-if="'user'===type" @click="addDepartUser">确定</vxe-button>
        <vxe-button status="primary" v-if="'role'===type" @click="addRole">确定</vxe-button>
        <vxe-button status="primary" v-if="'dept'===type" @click="addDepart">确定</vxe-button>
      </template>
    </vxe-modal>
  </div>
</template>

<script>
import {mixin} from '@/utils/mixin.js'
import DepartUser from '@/components/sys/DepartUser.vue'
import SysRole from "@/components/sys/SysRole.vue";
import SysDepart from "@/components/sys/SysDepart.vue";

export default {
  name: "OrgPicker",
  mixins: [mixin],
  components: {
    SysDepart,
    SysRole,
    DepartUser
  },
  props: {
    title: {
      default: '请选择',
      type: String
    },
    type: {
      default: 'user', //  user-选人  dept-选部门 role-选角色
      type: String
    },
    multiple: { //是否多选
      default: false,
      type: Boolean
    },
    selected: {
      default: () => {
        return []
      },
      type: Array
    },
  },
  data() {
    return {
      loadingmodal1: false,
      companyId: '',
      companys: [], //用户所属的全部企业
      tableData2: [],//不能选择的用户
    }
  },
  created() {
    // this.companyId = this.userinfo?.userDepart?.companyId || ''
    this.companyId = '1386982800826626049'
  },
  methods: {
    addRole(val) {
      let that = this
      let data = this.$refs.sysRole.getData()
      if (val && val.length) {
        data = val
      }
      if (!data || data.length === 0) {
        this.$notification.warning({message: '请至少选择一个角色'});
        return;
      }
      if (data && data.length > 1 && !this.multiple) {
        this.$notification.warning({message: '请选择一个角色'})
        return
      }
      let newData = []
      if (data.length) {
        data.forEach(function (item, index) {
          newData.push({id: item.roleid, roleName: item.rolename || item.roleName})
        })
      }
      this.$emit('ok', newData)
      this.$refs.modal1.close()
    },
    addDepart(val) {
      let that = this
      let data = this.$refs.sysDepart.getData()
      if (val && val.length) {
        data = val
      }
      if (!data || data.length === 0) {
        this.$notification.warning({message: '请至少选择一个部门'});
        return;
      }
      if (data && data.length > 1 && !this.multiple) {
        this.$notification.warning({message: '请选择一个部门'})
        return
      }
      let newData = []
      if (data.length) {
        data.forEach(function (item, index) {
          newData.push({departId: item.departId, departName: item.name, departCode: item.id})
        })
      }
      this.$emit('ok', newData)
      this.$refs.modal1.close()

    },
    addDepartUser(val) {
      let that = this
      let data = this.$refs.departUser.getData()
      if (val && val.length) {
        data = val
      }
      if (!data || data.length === 0) {
        this.$notification.warning({message: '请至少选择一个人员'});
        return;
      }
      if (data && data.length > 1 && !this.multiple) {
        this.$notification.warning({message: '请选择一个人员'})
        return
      }

      let newData = []
      if (data.length) {
        data.forEach(function (item, index) {
          newData.push({userId: item.userId, name: item.realname, avatar: item.avatar})
        })
      }
      this.$emit('ok', newData)
      // this.$emit('ok', Object.assign([], newData.map(v => {
      //   v.name = v.realname
      //   return v
      // })))
      this.$refs.modal1.close()
    },

    async show() {
      await this.$refs.modal1.open();
    },
  }
}
</script>

<style lang='scss' scoped>


</style>